package com.health.db.dao.user.impl;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IUserAccountLedgerDao;
import com.health.db.mapper.UserAccountLedgerMapper;
import com.health.db.model.UserAccountLedger;
import com.health.redis.RedisConstants;

public class UserAccountLedgerDaoImpl extends ServiceInterface implements IUserAccountLedgerDao {

 
	UserAccountLedgerMapper userAccountLedgerMapper;

	String selectByAccountId=":accountId:";
	String selectByUserId=":accountIdBase:";
	@Override
	public int insertAccountLedger(UserAccountLedger record) {
		// TODO Auto-generated method stub
		int result=-1;
		result=userAccountLedgerMapper.insert(record);
		
		return result;
	}

	/**
	 * 
	 * userId 主键 userId
	 */
	@Override
	public UserAccountLedger selectByUserId(Long userId) {
		// TODO Auto-generated method stub
 
		    getRedisOperatiorLogic();
			String key=RedisConstants.UserAccountLedgerMapper+selectByUserId+userId.longValue();
			
			Object object=null;
			UserAccountLedger userAccountLedger=null;
			
			if(redisOperatiorLogic.isJedisIsConnected())
			{	
				//get data from redis
				object = redisOperatiorLogic.readObject(key, getPublicRedisGroupNo("1"));
			 }
		
		     if(null!=object){
		    	if (!object.equals("")) {
		    		 userAccountLedger =  (UserAccountLedger) object;
				}
		     }
		     else{
			
		    	 userAccountLedger =userAccountLedgerMapper.selectByUserId(userId);
					
			    if(redisOperatiorLogic.isJedisIsConnected())
				{
					redisOperatiorLogic.setEx(key, userAccountLedger,  getOrderRedisGroupNo("1"));
				}
			 }
 
		return userAccountLedger;
	}

	@Override
	public int updatAccountLedgerByUserIdSelective(UserAccountLedger record) {
		int result=-1;
 
		result=userAccountLedgerMapper.updatAccountLedgerByUserIdSelective(record);
		
		String key=RedisConstants.UserAccountLedgerMapper+selectByAccountId+record.getAccountid().longValue();
		
		String key1=RedisConstants.UserAccountLedgerMapper+selectByUserId+record.getAccountid().longValue();
		
		
		getRedisOperatiorLogic();
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(key,getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key1,getOrderRedisGroupNo("1"));
		}
		return result;
	}

	@Override
	public int updateBalance(UserAccountLedger record) {
		
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		int result=-1;
		String key=RedisConstants.UserAccountLedgerMapper+selectByAccountId+record.getAccountid();
		
		String key1=RedisConstants.UserAccountLedgerMapper+selectByUserId+record.getAccountid().longValue();
	
		result=userAccountLedgerMapper.updateBalance(record);
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(key,getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key1,getOrderRedisGroupNo("1"));
		}
		return result;
	}

	@Override
	public UserAccountLedger selectUserAccountLedgerByUserId(Long userId) {
		// TODO Auto-generated method stub
		getRedisOperatiorLogic();
		String key=RedisConstants.UserAccountLedgerMapper+selectByAccountId+userId.longValue();
		Object  object=null;
		UserAccountLedger userAccountLedger=null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{	
			//get data from redis
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		 
		 }
		  if(null!=object){
			  if (!object.equals("")) {
				  userAccountLedger =  (UserAccountLedger) object;
			  }
		   }
		  else
		  {
			  userAccountLedger =userAccountLedgerMapper.selectUserAccountLedgerByUserId(userId);
			  
			  if(redisOperatiorLogic.isJedisIsConnected())
				{ redisOperatiorLogic.setEx(key, userAccountLedger,  getOrderRedisGroupNo("1"));}
		  }
		return userAccountLedger;
	}

	/********************************************************************************************************************************/
	public UserAccountLedgerMapper getUserAccountLedgerMapper() {
		return userAccountLedgerMapper;
	}

	public void setUserAccountLedgerMapper(
			UserAccountLedgerMapper userAccountLedgerMapper) {
		this.userAccountLedgerMapper = userAccountLedgerMapper;
	}

	
	
}
